package com.example.wuyeguanli.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wuyeguanli.R;
import com.example.wuyeguanli.entiy.Complaint;
import com.example.wuyeguanli.entiy.Response;
import com.example.wuyeguanli.tools.ApiClient;
import com.example.wuyeguanli.tools.ComplaintAdapter;
import com.example.wuyeguanli.tools.DataValidator;

import java.util.ArrayList;
import java.util.List;

public class ComplaintListActivity extends AppCompatActivity {
    private final String TAG = "ApiClient";
    private RecyclerView recyclerView;
    private ComplaintAdapter adapter;
    private List<Complaint> complaintList;
    private Button btnAdd, btnBack, btnSearch;
    private EditText editRoom; // 虽然叫edit_room，但我们用来搜索业主手机号
    private ProgressBar loadingProgress;
    private final ApiClient apiClient = new ApiClient();
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 使用您新设计的布局文件
        setContentView(R.layout.activity_complaintlist); // 假设您的布局文件名叫这个
        if(getSupportActionBar() != null) getSupportActionBar().hide();

        initViews();
        setupRecyclerView();
        setupClickListeners();
    }

    private void initViews() {
        recyclerView = findViewById(R.id.recyclerView);
        btnAdd = findViewById(R.id.btn_add);
        btnBack = findViewById(R.id.btn_back);
        editRoom = findViewById(R.id.edit_room);
        btnSearch = findViewById(R.id.btn_search);
        loadingProgress = findViewById(R.id.loading_progress);
    }

    private void setupRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        complaintList = new ArrayList<>();
        adapter = new ComplaintAdapter(complaintList, this);
        recyclerView.setAdapter(adapter);
    }

    private void setupClickListeners() {
        btnBack.setOnClickListener(v -> finish());
        btnAdd.setOnClickListener(v -> {
            Intent intent = new Intent(this, ComplaintSuggestionActivity.class);
            startActivity(intent);
        });
        btnSearch.setOnClickListener(v -> {
            String phoneToSearch = editRoom.getText().toString();
            if (DataValidator.isValidPhoneNumber(phoneToSearch)) {
                loadComplaints(phoneToSearch);
            } else {
                Toast.makeText(this, "请输入有效的11位手机号", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void loadComplaints(String ownerPhone) {
        setLoadingState(true);
        // 你需要在ApiClient中添加一个getComplaintsByOwner的方法
        apiClient.getComplaintsByOwner(ownerPhone, new ApiClient.OnCompleteListener<Response<List<Complaint>>>() {
            @Override
            public void onSuccess(Response<List<Complaint>> data) {
                setLoadingState(false);
                if (data != null && "success".equals(data.getState())) {
                    complaintList.clear();
                    if (data.getData() != null) {
                        complaintList.addAll(data.getData());
                    }
                    adapter.notifyDataSetChanged();
                    Toast.makeText(ComplaintListActivity.this, "列表加载成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ComplaintListActivity.this, data != null ? data.getError() : "加载失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void OnError(Throwable error) {
                setLoadingState(false);
                Log.e(TAG, "LoadComplaints OnError: ", error);
                Toast.makeText(ComplaintListActivity.this, "网络错误，请稍后重试", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setLoadingState(boolean isLoading) {
        loadingProgress.setVisibility(isLoading ? View.VISIBLE : View.GONE);
        btnSearch.setEnabled(!isLoading);
        btnAdd.setEnabled(!isLoading);
        editRoom.setEnabled(!isLoading);
    }
}